package com.itboye.jmsfhz.main;

import java.util.Observable;
import java.util.Observer;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.itboye.baidu.util.NetWorkUtil;
import com.itboye.baidu.util.TipsToast;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.cache.ACache;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.jmsfhz.R;
import com.itboye.jmsfhz.app.MainApplication;
import com.itboye.jmsfhz.app.login.ui.LoginActivity;
import com.itboye.jmsfhz.app.logincontroller.LoginController;
import com.itboye.jmsfhz.app.logincontroller.LoginedState;
import com.itboye.jmsfhz.app.logincontroller.UnLoginState;
import com.itboye.jmsfhz.app.ui.main.MainPresenter;
import com.zihao.receiver.NetState;

@SuppressLint("NewApi")
public class MainActivity extends BaseActivity implements Observer,
		OnClickListener, android.widget.CompoundButton.OnCheckedChangeListener {

	private Button btnTest;
	private static TipsToast tipsToast;
	int temp;
	private MainPresenter presenter;
	ACache aCache;// 缓存类
	RadioGroup group;
	public Fragment[] fr;
	// private RadioGroup tab_radiogroup;
	private RadioButton[] mTabs;
	private int index;
	private int mCurrIndex;

	private ProgressDialog mProgressDialog;
	private final static String TAGS[] = new String[] { "near", "conv",
			"paotui", "wo" }; // fragments的tag
	private final static int Tab_Cnt = 4; // 5个tab页

	public void generate() {
		int baseW = 640;
		int baseH = 960;
		String addition = "";

	}

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			System.out.println("执行了2");
			index = 3;
			temp = index;
			setCurrentTab();
		}
	};

	@Override
	public void onMyCreate() {
		// RelativeLayout fragment_container =
		IntentFilter filter = new IntentFilter("set");
		registerReceiver(receiver, filter);
		aCache = ACache.get(this);// 得到缓存对象
		if ((Boolean) SPUtils.get(MainApplication.context, null,
				SpPublicParams.IS_LOGINED, false)) {
			LoginController.setLoginState(new LoginedState());
		} else {
			LoginController.setLoginState(new UnLoginState());
		}

		generate();
		initUI();
		initFragments();
	}

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_main;
	}

	@Override
	protected void onSaveInstanceState(Bundle arg0) {
		// TODO Auto-generated method stub
		// super.onSaveInstanceState(arg0);不注释掉，一旦程序出现bug,界面将出现重叠
	}

	@Override
	public void update(Observable observable, Object data) {

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {

		default:
			break;
		}

	}

	private void initUI() {
		// tab_radiogroup = (RadioGroup) findViewById(R.id.tab_radiogroup);
		mTabs = new RadioButton[Tab_Cnt];
		mTabs[0] = (RadioButton) findViewById(R.id.radio_button0);
		mTabs[1] = (RadioButton) findViewById(R.id.radio_button1);
		mTabs[2] = (RadioButton) findViewById(R.id.radio_button2);
		// mTabs[3] = (RadioButton) findViewById(R.id.radio_button3);
		mTabs[3] = (RadioButton) findViewById(R.id.radio_button4);
		for (RadioButton bt : mTabs) {
			bt.setOnCheckedChangeListener(this);
		}

	}

	private void setCurrentTab() {
		if (mCurrIndex != index) {
			FragmentTransaction trx = getSupportFragmentManager()
					.beginTransaction();
			trx.hide(fr[mCurrIndex]);
			if (!fr[index].isAdded()) {
				trx.replace(R.id.fragment_container, fr[index]);
			}
			trx.show(fr[index]).commitAllowingStateLoss();
			mTabs[mCurrIndex].setChecked(false);
			mCurrIndex = index;
			if (!mTabs[mCurrIndex].isChecked()) {
				mTabs[mCurrIndex].setChecked(true);// 不会重复触发setCurrentTab
			}

		}
	}

	void initFragments() {

		fr = new Fragment[] { new RoofFragment(), new EmploymentFragment(),
				new ProjectFragment(), new WoMengFragment(),
				new WoMengFragment() };

		getSupportFragmentManager().beginTransaction()
				.add(R.id.fragment_container, fr[0], TAGS[0])
				.add(R.id.fragment_container, fr[1], TAGS[1])
				.add(R.id.fragment_container, fr[2], TAGS[2])
				.add(R.id.fragment_container, fr[3], TAGS[3]).hide(fr[1])
				.hide(fr[2]).hide(fr[3]).hide(fr[4]).show(fr[mCurrIndex])
				.commit();
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			switch (buttonView.getId()) {
			case R.id.radio_button0:
				index = 0;
				break;
			case R.id.radio_button1:
				String uid = (String) SPUtils.get(MainActivity.this, null,
						SpPublicParams.UID, "");
				System.out.println("uiduiduid" + uid);
				if (uid == null || uid == "") {
					Intent intent = new Intent(MainActivity.this,
							LoginActivity.class);
					startActivity(intent);
				}
				index = 1;
				break;
			case R.id.radio_button2:
				String uidTwo = (String) SPUtils.get(MainActivity.this, null,
						SpPublicParams.UID, "");
				if (uidTwo == null || uidTwo == "") {
					Intent intent = new Intent(MainActivity.this,
							LoginActivity.class);
					startActivity(intent);
				}
				index = 2;
				break;
			case R.id.radio_button4:
				index = 3;
				try {
					WoMengFragment.setPosition();
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			default:
				break;
			}
			temp = index;
			setCurrentTab();
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(receiver);
	}

	private long exitTime = 0;

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			if ((System.currentTimeMillis() - exitTime) > 2000) {
				ByAlert.alert("再按一次退出程序");
				exitTime = System.currentTimeMillis();
			} else {
				// 退出代码
				finish();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
